package fristphase.day9_06.mysubmarinegame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class World extends JPanel{
    public static final int WIDTH = 641;
    public static final int HEIGHT = 479;
    private static final int RUNNING = 0;
    private static final int PAUSE = 1;
    private static final int GAME_OVER = 2;
    private int state = RUNNING;

    BattleShip ship = new BattleShip();
    SeaObject[] submarines = {};
    Mine[] mines = {};
    Bomb[] bombs = {};

    //生产潜艇
    public SeaObject nextSubmarine(){
            Random random = new Random();
            int type = random.nextInt(20);
            if(type < 10){
                return new ObserveSubmarine();
            }else if(type < 15){
                return new  MineSubmarine();
            }else{
                return new TorpedoSubmarine();
            }
    };

    //水雷入场
    private int mineIndex = 0;
    public void mineEnterAction(){
        mineIndex++;
        if(mineIndex%100==0){
            for (int i = 0; i < submarines.length; i++) {
                SeaObject s = submarines[i];
                if(s instanceof MineSubmarine){
                    MineSubmarine obj = (MineSubmarine) s;
                    mines = Arrays.copyOf(mines,mines.length+1);
                    mines[mines.length-1] = obj.shootMine();
                }
            }
        }
    }




    //潜艇入场
    private int submarineIndex = 0;
    private void submarineEnterAction(){
        submarineIndex ++;
        if(submarineIndex%40 == 0){
            submarines = Arrays.copyOf(submarines,submarines.length+1);
            submarines[submarines.length-1] = nextSubmarine();
        }
    }

    //海洋对象移动
    private void seaObjectMove(){
        for (int i = 0; i < submarines.length; i++) {
            submarines[i].move();
        }
        for (int i = 0; i < mines.length; i++) {
            mines[i].move();
        }
        for (int i = 0; i < bombs.length; i++) {
            bombs[i].move();
        }
    }

    //删除出界海洋对象
    private void isDeleteSeaObject(){
        for (int i = 0; i < submarines.length; i++) {
            if(submarines[i].isDeleteSubmarineJudge() || submarines[i].isDead()){
                submarines[i] = submarines[submarines.length-1];
                submarines = Arrays.copyOf(submarines,submarines.length-1);
            }
        }
        for (int i = 0; i < mines.length; i++) {
            if(mines[i].deleteMineJudge() || mines[i].isDead()){
                mines[i] = mines[mines.length-1];
                mines = Arrays.copyOf(mines,mines.length-1);
            }
        }
        for (int i = 0; i < bombs.length; i++) {
            if(bombs[i].deleteBombJudge() || bombs[i].isDead()){
                bombs[i] = bombs[bombs.length-1];
                bombs = Arrays.copyOf(bombs,bombs.length-1);
            }

        }
    }

    //炸弹爆炸判断
    private int score = 0;
    private void bombBangAction(){
        for (int i = 0; i < bombs.length; i++) {
            Bomb b = bombs[i];
            for (int j = 0; j < submarines.length; j++) {
                SeaObject s = submarines[j];
                if(s.isHit(b) && s.isLive() && b.isLive()){
                    s.goDead();
                    b.goDead();
                    if(s instanceof EnemyLife){
                        EnemyLife obj = (EnemyLife) s;
                        ship.addLife(obj.getLife());
                    }else if(s instanceof EnemyScore){
                        EnemyScore obj = (EnemyScore) s;
                        score += obj.getScore();
                    }
                }
            }
        }
    }

    //水雷爆炸判断
    private  void mineBangAction(){
        for (int i = 0; i < mines.length; i++) {
            Mine m = mines[i];
            if (m.isLive() && ship.isLive() && ship.isHit(m)){
                m.goDead();
                ship.subtractLife();
            }
        }
    }


    //检测游戏结束
    public void gameOver(){
        if(ship.getLife()<=0){
            state = GAME_OVER;
        }
    }

    public void action(){
        KeyAdapter k = new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode() == KeyEvent.VK_P){
                    if(state == RUNNING){
                        state = PAUSE;
                    }else if(state == PAUSE){
                        state = RUNNING;
                    }
                }
                if(state == RUNNING){
                    if(e.getKeyCode() == KeyEvent.VK_SPACE){
//                        System.out.println("-----------");
                        Bomb obj = ship.shootBomb();
                        bombs = Arrays.copyOf(bombs,bombs.length+1);
                        bombs[bombs.length-1] = obj;
                    }
                    if(e.getKeyCode() == KeyEvent.VK_LEFT){
                        ship.leftMove();
                    }
                    if(e.getKeyCode() == KeyEvent.VK_RIGHT){
                        ship.rightMove();
                    }
                }

            }
        };
        this.addKeyListener(k);

        Timer timer = new Timer();
        int interval = 10;
        timer.schedule(new TimerTask() {
            public void run() {
                if(state == RUNNING){
                    submarineEnterAction();//潜艇入场
                    mineEnterAction();//水雷入场
                    seaObjectMove();//海洋对象移动
                    isDeleteSeaObject();//删除越界对象
                    bombBangAction();//炸弹爆炸判断功能
                    mineBangAction();//水雷爆炸判断功能
                    gameOver();//游戏结束判断功能
                    repaint();
                }

            }
        },interval,interval);
    }

    //画各种海洋对象
    public void paint(Graphics g){
        Images.sea.paintIcon(null,g,0,0);
        ship.paintImage(g);
        for (int i = 0; i < submarines.length; i++) {
            submarines[i].paintImage(g);
        }
        for (int i = 0; i < mines.length; i++) {
            mines[i].paintImage(g);
        }
        for (int i = 0; i < bombs.length; i++) {
            bombs[i].paintImage(g);
        }
        g.drawString("SCORE："+score,200,50);
        g.drawString("LIFE："+ship.getLife(),400,50);
        if(state == GAME_OVER){
            Images.gameOver.paintIcon(null,g,0,0);
        }
    }




    public static void main(String[] args) {
        JFrame frame = new JFrame();
        World world = new World();
        world.setFocusable(true);
        frame.add(world);

        frame.setSize(WIDTH+6,HEIGHT+30);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(true);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);

        world.action();
    }
}
